<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:auxiliary:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:auxiliary:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="auxiliaryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">{{ (scope.$index + 1) }}</template>
      </el-table-column>

      <el-table-column label="辅助信息类别" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.type" :value="scope.row.type" />
        </template>
      </el-table-column>

      <el-table-column label="附件">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="viewAttachments(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:auxiliary:remove']"
            style="width: 60px"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公路建设监督系统辅助信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="人才ID" prop="talentId">
          <el-input v-model="form.talentId" placeholder="请输入人才ID" />
        </el-form-item>
        <el-form-item label="证件附件" prop="attachments">
          <el-upload
            class="upload"
            action="/api/uploadToOSS"
            :file-list="fileList"
            :http-request="uploadFile"
            :on-change="handleChange"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            multiple
          >
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          </el-upload>
          <img :src="this.url" class="image" />
          <span>格式为png,jpg,jpge,pdf</span>
        </el-form-item>
        <el-form-item label="类别" prop="type">
          <el-select v-model="form.type" placeholder="请选择证件类别" clearable>
            <el-option
              v-for="dict in dict.type.type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="附件列表" :visible.sync="dialogVisible" width="50%">
      <div
        v-for="(attach, index) in attachments"
        :key="index"
        style="margin-bottom: 10px;display: flex;justify-content: center;"
      >
        <img :src="attach.attach" alt />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAuxiliary,
  getAuxiliary,
  delAuxiliary,
  addAuxiliary,
  updateAuxiliary,
  getAttachment,
  uploadAttachment
} from "@/api/system/road";

export default {
  name: "Road",
  dicts: ["type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      dialogVisible: false,
      fileList: [],
      url: "",
      formData: [],
      attachments: [],
      // 总条数
      total: 0,
      // 公路建设监督系统辅助信息表格数据
      auxiliaryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        talentId: null,
        type: null,
        attach: null,
        createUser: null,
        createDate: null,
        updateUser: null,
        updateDate: null,
        status: null
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公路建设监督系统辅助信息列表 */
    getList() {
      this.loading = true;
      listAuxiliary(this.queryParams).then(response => {
        this.auxiliaryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.fileList = [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        talentId: null,
        attachs: [],
        type: null,
        attach: null,
        remark: null,
        createUser: null,
        createDate: null,
        updateUser: null,
        updateDate: null,
        status: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公路建设监督系统辅助信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAuxiliary(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公路建设监督系统辅助信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAuxiliary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAuxiliary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
      this.fileList = [];
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(
          '是否确认删除公路建设监督系统辅助信息编号为"' + ids + '"的数据项？'
        )
        .then(function() {
          return delAuxiliary(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/talent/road/export",
        {
          ...this.queryParams
        },
        `auxiliary_${new Date().getTime()}.xlsx`
      );
    },
    //自定义上传文件
    uploadFile(file) {
      // 创建FormData对象
      const formData = new FormData();
      // 添加文件到FormData对象中，'file'是后端接收文件参数的名称

      formData.append("file", file.file);
      uploadAttachment(formData).then(response => {
        this.form.attachs.push(response.msg);
      });
      console.log(formData);
    },
    // 文件列表变化
    handleChange(file, fileList) {
      this.fileList = fileList;
    },

    // 删除文件
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },

    // 点击预览文件
    handlePreview(file) {
      console.log(file);
    },
    viewAttachments(id) {
      getAttachment(id)
        .then(response => {
          this.attachments = response.rows;
          this.dialogVisible = true;
        })
        .catch(error => {
          console.error("Error fetching attachments:", error);
        });
    }
  }
};
</script>
